<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %> 
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>流程管理版本</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>
<body class="iframeBody">
<div class="Wrapper">
<table class="layout2col1">
        <tbody>
            <tr>
                <td valign="top">
                    <div class="sqPanel">
                       <!--  <div class="sqPanel_head">
                            <form id="searchBar2" class="titSearchBar">
                                <div class="fieldset1 clearFix">
                                    <div class="fieldBox">
                                        <div class="label">名称</div>
                                            <div class="field">
                                                 <input name="resNm" id="resNm" type="text" class="ui-textbox" value="" ligerui="width: 100">
                                            </div>
                                        </div>
                                    <span id="searchBtn2" class="sq-search-button"></i>查询</span>
                                </div>
                            </form>
                        </div> -->
                        <div class="sqPanel_body" style="min-height: 300px;">
                            <div class="FBL5">
                                <span id="saveFlowWithOrgRBtn" class="sq-button sq-button-round">保存</span>
                            </div>
                            <div class="nanoWrap" style="height: 320px;">
                                <div class="nano"><div class="nano-content">
                                <ul id="tree1">
                                </ul>
                                </div></div>
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
<input hidden="true" id='manageId' name="manageId" value="${manageId }"/>
<input hidden="true" id='procDefKey' name="procDefKey" value="${procDefKey }"/>
</div>

<script type="text/javascript">
	var tree,
	RESOURCE_IDs=[];
	$(function () {
		var _manageId = $("#manageId").val();
		tree = $("#tree1").ligerTree({
	    	//url: '<%=path%>/system/sysflowmanageorgr/initOrgTree.do?manageId=' + _manageId,
	        data:[],
	    	ajaxType: 'get',
	        nodeWidth: 300,
	        enabledCompleteCheckbox: false,
	        onCheck: function (item,checked) {
	           /*  if (!grid.getSelectedRow()) {
	                ligerDialogWarn("请选择角色！",1000);
	                tree.cancelSelect(item);
	                return;
	            }; */
	            var checkNodes=getCheckNodes(item.data);
	            setRESOURCE_IDs(checkNodes, checked);
	            //alert(RESOURCE_IDs);
	        },
	        onSuccess: function (data) {
	            setNanoScroller();
	        }
	    });
		
		initTreeData();
		
		//保存按钮
		$("#saveFlowWithOrgRBtn").click(function() {
			var _manageId = $("#manageId").val();
			var _procDefKey = $("#procDefKey").val();
			var params = {
					manageId:_manageId,
					orgCds:JSON.stringify(RESOURCE_IDs),
					procDefKey:_procDefKey
			}
			$.ligerDialog.confirm(LogUtils.LOG_CMM_SUBMIT_CONFIRM_CONTENT_001, function (flag) {
				if(flag == false)return;
				var _url = "<%=path%>/system/sysflowmanageorgr/modify.do";
				AjaxUtils.ajaxPost(_url, params, function(msg){
					if(msg.success == 1){
	    				 UIUtils.alertSuccess(msg.info);
	    			 }else{
	    				 UIUtils.alertError(msg.info);
	    			 }
				},true);
			});
		});
	});
	
	
	function initTreeData(){
		var _manageId = $("#manageId").val();
		var _url = '<%=path%>/system/sysflowmanageorgr/initOrgTree.do';
		var parms = {manageId:_manageId};
		AjaxUtils.ajaxGet(_url, parms, function(msg){
			if(msg.success == 1){
        		tree.setData(msg.data.tree);
                RESOURCE_IDs=msg.data.orgCdList;
        	}else{
        		UIUtils.alertError(msg.info);
        	}
		},true);
	}
	
	
    function setRESOURCE_IDs (checkNodes, ischecked) {
        if (ischecked) {
            for (var i = 0; i < checkNodes.length; i++) {
                var checkedID=checkNodes[i].id;
                if(RESOURCE_IDs.indexOf(checkedID)==-1) RESOURCE_IDs.push(checkedID);
            };
        } else{
            for (var i = 0; i < checkNodes.length; i++) {
                var checkedID=checkNodes[i].id;
                var index=RESOURCE_IDs.indexOf(checkedID);
                if(index>=0) RESOURCE_IDs.splice(index,1);
            };
        };
    }
    // 获取tree check或uncheck的时候,触发的所有node，包括子节点
    function getCheckNodes (data) {
        var checkNodes=[];
        function findNodes (node) {
            checkNodes.push(node);
            if (tree.hasChildren(node)) {
                for (var i = 0; i < node.children.length; i++) {
                    var childrenNode=node.children[i];
                    findNodes (childrenNode);
                };
            }else{
                return;
            };
        }
        findNodes (data);
        return checkNodes;
    }
    
    function setNanoScroller () {
        $(".nanoWrap .nano").nanoScroller({
            preventPageScrolling: true
        });
    }
	
	function iframeResize () {
	    if (parent.mainApp) {
	        parent.mainApp.parentTab.onResize();
	    };
	}
</script>
</body>
</html>